<template>
  <div class="page">
    <!-- 搜索区域 开始 -->
    <div class="el_card_box">
      <el-form :inline="true" size="small" :model="formInline" class="search_form">
        <el-form-item label="预约时间：">
          <el-date-picker v-model="formInline.orderTime" type="daterange" unlink-panels :picker-options="pickerOptions" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报名时间：">
          <el-date-picker v-model="formInline.applyTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="提醒时间：">
          <el-date-picker v-model="formInline.orderTime2" type="daterange" unlink-panels :picker-options="pickerOptions" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预约医院：">
          <!-- <el-select v-model="formInline.hospital" filterable clearable>
            <el-option v-for="(item,index) in hospitalList" :key="index" :label="item.name" :value="item.id"></el-option>
          </el-select> -->
          <hospital-select v-model="formInline.hospital"></hospital-select>
        </el-form-item>
        <el-form-item label="城市：">
          <!-- <el-select v-model="formInline.city" filterable clearable>
            <el-option v-for="(item,index) in cityList" :key="index" :label="item.name" :value="item.code"></el-option>
          </el-select> -->
          <city-select v-model="formInline.cities"></city-select>
        </el-form-item>
        <el-form-item label="报名费：">
          <el-select v-model="formInline.enrollFeeHandle" filterable clearable>
            <el-option v-for="(item,index) in DEALENROLLFEELIST" :key="index" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收款情况：">
          <el-select v-model="formInline.dealType" filterable clearable>
            <el-option v-for="(item,index) in DEALRECORDTYPELIST" :key="index" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="成交项目：">
          <!-- <el-select v-model="formInline.project" filterable clearable>
            <el-option v-for="(item,index) in projectList" :key="index" :label="item.name" :value="item.id"></el-option>
          </el-select> -->
          <project-select v-model="formInline.projects"></project-select>
        </el-form-item>
        <el-form-item label="负责人：">
          <!-- <el-select v-model="formInline.principal" filterable clearable>
            <el-option v-for="(item,index) in userList" :key="index" :label="item.name" :value="item.id"></el-option>
          </el-select> -->
          <user-select v-model="formInline.principals"></user-select>
        </el-form-item>
        <el-form-item label="广告代号：">
          <!-- <el-select v-model="formInline.adSource" filterable clearable>
            <el-option v-for="(item,index) in adCodeList" :key="index" :label="item.name" :value="item.id"></el-option>
          </el-select> -->
          <adcode-select v-model="formInline.adSources"></adcode-select>
        </el-form-item>
        <el-form-item label="关键词：">
          <el-input v-model="formInline.term" placeholder="请输入客户姓名/电话"></el-input>
        </el-form-item>
      </el-form>
      <div class="search_btn_box">
        <el-button type="primary" size="small" @click="onSearch">筛选</el-button>
        <el-button type="default" size="small" @click="rest">清空筛选</el-button>
      </div>
    </div>
    <!-- 搜索区域 end -->

    <!-- 表格 开始 -->
    <div class="table_box">

      <!-- 操作区域 开始 -->
      <div class="action_box">
        <auth-button type="default" uid="10801801" size="small" :loading="eloading" @click.native="downloadExcel">导出excel</auth-button>
      </div>
      <!-- 操作区域 end -->

      <el-table ref="elTable" class="el_table" size="small" :data="tableData">
        <el-table-column prop="channelName" label="医院信息">
          <template slot-scope="{row}">
            <tab-hospital :user="showFormatter(row)"></tab-hospital>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="客户信息" show-overflow-tooltip />
        <el-table-column prop="appointmentTime" label="预约时间" show-overflow-tooltip />
        <el-table-column prop="remindDatetime" label="提醒操作时间" show-overflow-tooltip></el-table-column>
        <el-table-column prop="principal" label="负责人" show-overflow-tooltip width="80" />
      </el-table>
      <div class="pagination_list">
        <el-pagination background layout="sizes, total,prev, pager, next, jumper" :total="pageTotal" :page-size="pageSize" :page-sizes="pageSizes" :current-page.sync="pageNumber" @current-change="getList" @size-change="changePageSize" />
      </div>
    </div>
    <!-- 表格 end -->

  </div>
</template>

<script>
import { GETDEALSEARCH, DEALEXPORT } from "@/http/deal";
import {
  GETCITYOPTIONS,
  GETUSEROPTIONS,
  GETPROJECTOPTIONS,
  GETADSOURCEOPTIONS,
  GETDEALCOLLECTLOGSEARCH,
  GETORDERREMINDSEARCH,
  GETREMINDEXPORT
} from "@/http/system";
import { GETHOSPITALOPTIONS } from "@/http/hospital";
import {
  showToast,
  showModal,
  deepClone,
  broadcast,
  received,
} from "@/common/util";
import { exportExcel } from "@/common/common";
import GlsxDialog from "@/components/Dialog/index";
import moment from "moment";
import TabHospital from "@/components/hospital";
import {
  DEALTYPELIST,
  DEALENROLLFEELIST,
  DEALTYPE,
  DEALRECORDTYPELIST,
} from "@/common/constants";
import AuthButton from "@/components/auth-button";
import CitySelect from "@/components/select/city";
import ProjectSelect from "@/components/select/project";
import UserSelect from "@/components/select/user";
import AdcodeSelect from "@/components/select/adCode";
import HospitalSelect from "@/components/select/hospital";
export default {
  components: {
    GlsxDialog,
    TabHospital,
    AuthButton,
    CitySelect,
    ProjectSelect,
    UserSelect,
    AdcodeSelect,
    HospitalSelect,
  },
  data() {
    return {
      formInline: {},
      tableData: [],
      pageTotal: 0,
      pageNumber: 1,
      pageSizes: [10, 20, 50, 100],
      pageSize: 20,
      orderTime: [],
      orderTime2: [],
      showDialog: false,
      loading: false,
      pickerOptions: {
        shortcuts: [
          {
            text: "前天",
            onClick(picker) {
              const end = moment().subtract(2, "days").format("YYYY-MM-DD");
              const start = moment().subtract(2, "days").format("YYYY-MM-DD");
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const end = moment().subtract(1, "days").format("YYYY-MM-DD");
              const start = moment().subtract(1, "days").format("YYYY-MM-DD");
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "今天",
            onClick(picker) {
              const end = moment().subtract(0, "days").format("YYYY-MM-DD");
              const start = moment().subtract(0, "days").format("YYYY-MM-DD");
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "明天",
            onClick(picker) {
              const end = moment().add(1, "days").format("YYYY-MM-DD");
              const start = moment().add(1, "days").format("YYYY-MM-DD");
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "后天",
            onClick(picker) {
              const end = moment().add(2, "days").format("YYYY-MM-DD");
              const start = moment().add(2, "days").format("YYYY-MM-DD");
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      cityList: [],
      eloading: false,
      projectList: [],
      userList: [],
      hospitalList: [],
      DEALTYPELIST: DEALTYPELIST,
      DEALENROLLFEELIST: DEALENROLLFEELIST,
      DEALRECORDTYPELIST: DEALRECORDTYPELIST,
      adCodeList: [],
    };
  },
  mounted() {
    this.getList();
    //this.getCity();
    //this.getHospital();
    //this.getProject();
    //this.getUser();
    //this.getAdCodeList();
    received("commom.edit.success", this.getList);
  },
  methods: {
    /** 重置 */
    rest() {
      this.formInline = {};
      this.orderTime = [];
      this.orderTime2 = [];
      this.getList();
    },

    /** 每页展示 */
    changePageSize(e) {
      this.pageSize = e;
      this.getList();
    },

    /** 获取参数 */
    getParams() {
      let oParams = JSON.parse(JSON.stringify(this.formInline));
      let n = oParams.orderTime;
      let m = oParams.orderTime2;
      let nm = oParams.applyTime;

      if (n && n.length > 0) {
        oParams.orderStartTime = n[0];
        oParams.orderEndTime = n[1];
      }

      if (m && m.length > 0) {
        oParams.remindStartTime = m[0];
        oParams.remindEndTime = m[1];
      }

      if (nm && nm.length > 0) {
        oParams.enrollStartTime = nm[0];
        oParams.enrollEndTime = nm[1];
      }

      delete oParams.orderTime;
      delete oParams.orderTime2;
      delete oParams.applyTime;

      oParams.pageNumber = this.pageNumber;
      oParams.pageSize = this.pageSize;
      return oParams;
    },

    /** 获取列表 */
    getList() {
      let _this = this;
      let oParams = this.getParams();
      GETORDERREMINDSEARCH(oParams).then((res) => {
        let obj = res.data || {};
        _this.tableData = obj.list || [];
        _this.pageTotal = obj.totalElements;
      });
    },

    // 获取所有城市
    getCity() {
      GETCITYOPTIONS().then((res) => {
        this.cityList = res.data || [];
      });
    },

    // 格式化处理用户信息
    userFormatter(row) {
      let o = deepClone(row);
      o.customerName = o.name;
      o.customerPhone = o.phone;
      o.id = o.clueId;
      return o;
    },

    // 监听组件变化
    onChange(e) {
      if (e.id) {
        this[e.id] = false;
      }
    },

    // 获取所有项目
    getProject() {
      GETPROJECTOPTIONS().then((res) => {
        this.projectList = res.data || [];
      });
    },

    // 获取所有医院
    getHospital(code) {
      let oParams = {};
      oParams.city = code;
      GETHOSPITALOPTIONS(oParams).then((res) => {
        this.hospitalList = res.data || [];
      });
    },

    // 获取负责人列表
    getUser(name) {
      GETUSEROPTIONS({ username: name }).then((res) => {
        this.userList = res.data || [];
      });
    },

    // 收款明细
    lookDetail(row) {
      this.$router.push({ name: "dealPayment", query: { id: row.id } });
    },

    // 格式化展示时间
    showTime(str) {
      if (!str) {
        return "";
      }
      return moment(str).format("yyyy-MM-DD HH:mm");
    },

    // 获取广告代号
    getAdCodeList(id) {
      GETADSOURCEOPTIONS().then((res) => {
        this.adCodeList = res.data || [];
      });
    },

    // 格式化医院数据
    showFormatter(row) {
      if (!row) {
        return {};
      }
      row.hospitalName = row.hospitalName;
      row.hospitalAddress = row.address;
      return row;
    },

    // 导出excel
    downloadExcel() {
      let _this = this;
      let o = deepClone(_this.formInline);
      let m = Object.keys(o);
      if (m.length > 0) {
        showModal({
          message: "确定是否导出筛选条件下面所有客户列表?",
          showCancelButton: true,
          type: "warning",
          success: function () {
            _this.dowloadExcelFile();
          },
        });
        return false;
      }

      _this.dowloadExcelFile();
    },

    // 下载excel
    dowloadExcelFile() {
      let _this = this;
      let oParams = deepClone(this.formInline);
      let options = {};
      oParams.pageType = _this.id;
      if (oParams.enrollProject) {
        oParams.enrollProject = oParams.enrollProject.toString();
      }

      options.method = "GET";
      options.data = oParams;
      options.url = GETREMINDEXPORT;
      options.fileName = "提醒记录列表.xlsx";
      _this.eloading = true;
      exportExcel(options).then((res) => {
        _this.eloading = false;
      });
    },

    // 搜索
    onSearch(){
      this.pageNumber = 1;
      this.getList()
    }
  },
};
</script>

<style lang="scss" scoped>
@import "@/assets/style/customer/index.scss";
.action_box{
  margin-bottom:10px;
}
</style>
